package com.lsh.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@Slf4j
@MapperScan(basePackages="com.lsh.dao.**",sqlSessionTemplateRef="sqlSessionFactoryTemplate")
public class DatasourceConfig {
    public DatasourceConfig(){
        log.info("加载数据源配置类");
    }

    @Primary
    @Bean(name="DataSource1")
    @ConfigurationProperties(prefix="spring.datasource.db1")
    public DataSource getDataSourceBpm() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name="DataSource2")
    @ConfigurationProperties(prefix="spring.datasource.db2")
    public DataSource getDataSourcePas() {
        return DataSourceBuilder.create().build();
    }
    /**
     * 动态数据源
     * @param
     * @param
     * @return
     */
    @Bean(name="dynamicDataSource")
    public DynamicDataSource dataSource(@Qualifier("DataSource1") DataSource DataSource1,
                                        @Qualifier("DataSource2") DataSource DataSource2) {
        Map<Object,Object> targetDataSource = new HashMap<>();
        targetDataSource.put(DatabaseContextHolder.DataBaseType.DataBase1, DataSource1);
        targetDataSource.put(DatabaseContextHolder.DataBaseType.DataBase2, DataSource2);
        DynamicDataSource dataSource = new DynamicDataSource();
        dataSource.setTargetDataSources(targetDataSource);
        dataSource.setDefaultTargetDataSource(DataSource1);//默认数据源
        return dataSource;
    }
    /**
     * 创建工厂
     * @param dynamicDataSource
     * @return
     * @throws Exception
     */
    @Bean(name="sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource")DataSource dynamicDataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dynamicDataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath*:mybatis/mapper/**/*.xml"));
        return bean.getObject();
    }

    /**
     * 事务管理
     * @param dynamicDataSource
     * @return
     */
    @Bean("dataSourceTransactionManager")
    public DataSourceTransactionManager
    dataSourceTransactionManager(@Qualifier("dynamicDataSource")DataSource dynamicDataSource) {
        return new DataSourceTransactionManager(dynamicDataSource);
    }
    /**
     * 创建模板
     * @param sqlSessionFactory
     * @return
     */
    @Bean(name="sqlSessionFactoryTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory")SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}